package com.ui.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ddd.wandemo.R;

import java.util.ArrayList;
import java.util.List;

public class HotKeyRvAdapter extends RecyclerView.Adapter<HotKeyRvAdapter.HotKeyViewHolder> {
    private List<String> keyList = new ArrayList<>();
    private Context mContext;
    private OnItemClickListener mListener;
    public HotKeyRvAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public HotKeyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hot_key, parent, false);
        return new HotKeyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final HotKeyViewHolder holder,int position) {
        holder.tvHotKey.setText(keyList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(keyList.get(holder.getAdapterPosition()));
            }
        });
    }

    @Override
    public int getItemCount() {
        return keyList == null ? 0 : keyList.size();
    }

    class HotKeyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvHotKey;
        public HotKeyViewHolder(View itemView) {
            super(itemView);
            tvHotKey = itemView.findViewById(R.id.tv_hot_key);
        }
    }

    public void setData(List<String> data){
        keyList.clear();
        if (data != null) {
            keyList.addAll(data);
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void itemClick(String key);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }
}
